package com.self.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.self.serviceedu.client.VodClient;
import com.self.serviceedu.entity.EduVideo;
import com.self.serviceedu.mapper.EduVideoMapper;
import com.self.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author xiaozeng
 * @since 2020-09-16
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    //注入VODClient、
    @Autowired
    private VodClient vodClient;

    //根据课程Id删除小节
    // TODO 删除小节时删除对应视频
    @Override
    public void removeVideoByCourseId(String courseId) {

        //1.根据课程ID查询所有的视频Id
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        videoWrapper.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(videoWrapper);

        //List<EduVideo>变成List<String>
        ArrayList<String> videoIds = new ArrayList<>();
        for (EduVideo eduVideo : videoList) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                //加入videoIds中
                videoIds.add(videoSourceId);
            }

        }
        //如果videoIds为空时，没必要远程调用删除多个视频
        if(videoIds.size() > 0){
            vodClient.deleteBatch(videoIds);
        }

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
